/************************************************************
	File:		Target.cpp
	Author:		Doug Monroe (dmonroe@fullsail.com)
	Course:		SGD 1304
	Purpose:	CTarget class handles the destructible target entity
************************************************************/

#include "Target.h"

#include "../../SGD Wrappers/CSGD_TextureManager.h"
#include "../../SGD Wrappers/CSGD_EventSystem.h"
#include "../../SGD Wrappers/CSGD_MessageSystem.h"

#include "DestroyEntityMessage.h"


//***********************************************************
// CONSTRUCTOR
CTarget::CTarget(void)
{
	// Set the entity's type
	CEntity::m_eType = ENT_TARGET;

	// Load explosion image
	m_nExplosionID = CSGD_TextureManager::GetInstance()->LoadTexture( _T("../resource/graphics/SGD_Explosion.png") );
	m_fExplosionTimer = 0.0f;
}


//***********************************************************
// DESTRUCTOR
CTarget::~CTarget(void)
{
	CSGD_TextureManager::GetInstance()->UnloadTexture( m_nExplosionID );
	m_nExplosionID = -1;
}

//***********************************************************
// Update
//	- self-destruct when timer reaches 0
/*virtual*/ void CTarget::Update( float fElapsedTime )
{
	// Move
	CEntity::Update( fElapsedTime );


	// Is the timer running?
	if( m_fExplosionTimer > 0.0f )
	{
		m_fExplosionTimer -= fElapsedTime;
		
		SetWidth( 0 );
		SetHeight( 0 );

		// Have we reached the end?
		if( m_fExplosionTimer <= 0.0f )
		{
			// Self-destruct
			CDestroyEntityMessage* pMsg = new CDestroyEntityMessage( this );
			CSGD_MessageSystem::GetInstance()->SendMsg( pMsg );
			pMsg = nullptr;
		}
	}
}

//***********************************************************
// Render
//	- render the explosion image
/*virtual*/ void CTarget::Render( void )
{
	// Render explosion?
	if( m_fExplosionTimer > 0.0f )
	{
		CSGD_TextureManager::GetInstance()->Draw(
			m_nExplosionID, (int)GetPosX(), (int)GetPosY(), 
			1.0f, 1.0f, nullptr, 
			(float)(GetWidth()/2), (float)(GetHeight()/2), GetRotation() );
	}
	else 
		CEntity::Render();	// Render default
}

//***********************************************************
// CheckCollision
//	- CShip		- Explosion!
//	- CTurret	- Explosion!
//	- CLaser	- Explosion! +Points
/*virtual*/ bool CTarget::CheckCollision( IEntity* pOther )
{
	// Did we collide?
	if( CEntity::CheckCollision( pOther ) == true )
	{
		// Explosion!
		m_fExplosionTimer = 1.0f;


		// Was it a laser?
		if( pOther->GetType() == ENT_LASER )
		{
			// The ship earns 1 point
			int nPoints = 1;

			// Send the event IMMEDIATELY to use the local variable
			CSGD_EventSystem::GetInstance()->SendEventNow(
				"INCREASE_SCORE", &nPoints, nullptr, this );
		}
	}

	return false;
}
